package com.hoshiicloud.payment.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.payment.Paylist;
import com.hoshiicloud.payment.param.PaylistQueryParam;
import com.hoshiicloud.payment.service.PaylistService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 *  控制器
 * </p>
 *
 * @author Mr.wu
 * @since 2019-05-13
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/payment/paylist")
@Api(value = "支付订单", tags = {"支付订单"})
public class PaylistController {
    @Autowired
    private PaylistService paylistService;

    /**
    * @param param
    * @return com.hoshiicloud.common.rpc.Response
    * @description: 分页查询
    */
    @ApiOperation(value = "分页查询")
    @PostMapping("/query/page")
    public Response page(@RequestBody @Valid PaylistQueryParam param) {
        try {
            log.info("分页查询");
            QueryWrapper<Paylist> queryWrapper = new QueryWrapper<Paylist>();
            queryWrapper.eq(StringUtils.isNotBlank(param.getOrderId()), "order_id", param.getOrderId());
            queryWrapper.eq(param.getUserid() != null, "userid", param.getUserid());
            queryWrapper.eq(StringUtils.isNotBlank(param.getPayOrderno()), "pay_orderno", param.getPayOrderno());
            queryWrapper.eq(param.getPayType() != null, "pay_type", param.getPayType());
            queryWrapper.eq(StringUtils.isNotBlank(param.getPayOutderno()), "pay_outderno", param.getPayOutderno());
            queryWrapper.eq(param.getPaytime() != null, "paytime", param.getPaytime());
            queryWrapper.eq(param.getPayamount() != null, "payamount", param.getPayamount());
            queryWrapper.eq(param.getFees() != null, "fees", param.getFees());
            queryWrapper.eq(param.getAmount() != null, "amount", param.getAmount());
            queryWrapper.eq(param.getStatus() != null, "status", param.getStatus());
            queryWrapper.eq(StringUtils.isNotBlank(param.getInAccount()), "in_account", param.getInAccount());
            queryWrapper.eq(StringUtils.isNotBlank(param.getOutAccount()), "out_account", param.getOutAccount());
            queryWrapper.eq(param.getChannel() != null, "channel", param.getChannel());
            queryWrapper.eq(StringUtils.isNotBlank(param.getRefno()), "refno", param.getRefno());
            queryWrapper.eq(StringUtils.isNotBlank(param.getCallbackMessage()), "callback_message", param.getCallbackMessage());
            queryWrapper.eq(StringUtils.isNotBlank(param.getPlatformId()),"platformid",param.getPlatformId());
            IPage<Paylist> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            queryWrapper.select(BeanUtils.getSqlSelect(Paylist.class));
            return Response.data(paylistService.page(page, queryWrapper));
          } catch (ServiceException e) {
            return Response.failed(e.getError().getMsg());
          } catch (Exception e) {
            log.error("分页查询异常", e);
            return Response.failed("分页查询异常");
        }
    }
    /**
    * @param id
    * @return com.hoshiicloud.common.rpc.Response
    * @description: 根据id查询
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(paylistService.getById(id));
    }


    @GetMapping("/queryOneByPayOrderId/{payOrderId}")
    @ApiOperation(value = "根据退款单id查询退款支付流水")
    public Response<Paylist> queryOneByPayOrderId(@PathVariable("payOrderId") String payOrderId){
        return Response.data(paylistService.queryOneByPayOrderId(payOrderId));
    }



}
